var image = {};

image.init = function() {
    $('input[id=lefile]').change(function() {
        $('#photoCover').val($(this).val());
    });
};

image.add = function() {
    var url = $("form#addImage input[name=url]").val();
    var file = $('form#addImage #photoCover').val();
    var type = $('form#addImage [name=type]').val();
    var target = ($('form#addImage input[name=target]').val() == '') ? '0' : $('form#addImage input[name=target]').val();
    if (url !== '') {
        ajax({
            service: 'cpservice/image/add.json?type=' + type + '&target=' + target,
            data: {url: url},
            contentType: 'json',
            type: 'post',
            done: function(resp) {
                if (resp.success) {
                    popup.msg(resp.message, function() {
                        location.reload();
                    });
                } else {
                    popup.msg(resp.message);
                }
            }
        });
    } else if (file !== '') {
        ajaxUpload({
            service: 'cpservice/image/add.json?type=' + type + '&target=' + target,
            id: 'addImage',
            contentType: 'json',
            done: function(resp) {
                if (resp.success) {
                    popup.msg(resp.message, function() {
                        location.reload();
                    });
                } else
                    popup.msg(resp.message);
            }
        });
    }
};

image.addImage = function(_targetId, _type) {
    ajax({
        service: 'cpservice/image/getbytarget.json',
        data: {type: _type, target: _targetId},
        done: function(resp) {
            if (resp.success) {
                resp.targetId = _targetId;
                resp.type = _type;
                popup.open("popup-image-add", "Chi tiết hình ảnh", template('/cp/tpl/image/view.tpl', resp), [
                    {
                        title: 'Đóng',
                        style: 'btn-default',
                        fn: function() {
                            popup.close('popup-image-add');
                        }
                    }
                ]);
                $('#lefileadd').change(function() {
                    $('#photoCoveradd').val($(this).val());
                });
            } else {
                popup.msg(resp.message);
            }
        }
    });
};


image.changeType = function(obj, IdShow, IdNone) {
    if ($(obj).is(":checked")) {
        $("#" + IdShow).css({'display': 'block'});
        $("#" + IdNone).css({'display': 'none'});
    }
};

image.upload = function(_type) {
    var url = $("form#image-add-module input[name=urladd]").val();
    var type = $('form#image-add-module [name=type]').val();
    var target = ($('form#image-add-module input[name=target]').val() === '') ? '0' : $('form#image-add-module input[name=target]').val();
    if (_type === 'url') {
        ajax({
            service: 'cpservice/image/add.json?type=' + type + '&target=' + target,
            data: {url: url},
            contentType: 'json',
            type: 'post',
            done: function(resp) {
                if (resp.success) {
                    popup.msg(resp.message, function() {
                        popup.close('popup-image-add');
                        image.addImage(target, type);
                    });
                } else {
                    popup.msg(resp.message);
                }
            }
        });
    } else {
        ajaxUpload({
            service: 'cpservice/image/add.json?type=' + type + '&target=' + target,
            id: 'image-add-module',
            contentType: 'json',
            done: function(resp) {
                if (resp.success) {
                    popup.msg(resp.message, function() {
                        popup.close('popup-image-add');
                        image.addImage(target, type);
                    });
                } else
                    popup.msg(resp.message);
            }
        });
    }
};

image.deleteImage = function(_url, _index) {
    ajax({
        service: 'cpservice/image/delete.json',
        data: {url: _url},
        contentType: 'json',
        type: 'post',
        done: function(resp) {
            if (resp.success) {
                $("li[for=" + _index + "]").remove();
            } else {
                popup.msg(resp.message);
            }
        }
    });
};